package com.tourism.controller;


import com.tourism.core.common.utils.Result;
import com.tourism.domain.dto.menu.MenuQueryDTO;
import com.tourism.domain.entity.Menu;
import com.tourism.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单控制器
 *
 * @author lucky
 */
@RequestMapping("/api/admin/menu")
@RestController
public class MenuController {

    @Autowired
    private MenuService menuService;


    @GetMapping("nav")
    public Result<List<Menu>> getMenu() {
        return Result.success(menuService.getMenuNav());
    }


    @PostMapping("list")
    @PreAuthorize("hasAuthority('menu:list')")
    public Result<List<Menu>> getList(@RequestBody MenuQueryDTO menuQueryDTO) {
        return Result.success(menuService.getMenus(menuQueryDTO));
    }

    @PostMapping("save")
    @PreAuthorize("hasAuthority('menu:save')")
    public Result<Boolean> save(@RequestBody Menu menu) {
        return menuService.save(menu) ? Result.success() : Result.failure();
    }


    @PutMapping("update")
    @PreAuthorize("hasAuthority('menu:update')")
    public Result<Boolean> update(@RequestBody Menu menu) {
        return menuService.updateById(menu) ? Result.success() : Result.failure();
    }


    @DeleteMapping("delete")
    @PreAuthorize("hasAuthority('menu:delete')")
    public Result<Boolean> delete(@RequestBody List<Long> ids) {
        return menuService.removeBatchByIds(ids) ? Result.success() : Result.failure();
    }
}
